/**
 * 
 */
var userData = require("UserData");
var wxInterface = require("wxInterface");
cc.Class({
    extends: cc.Component,

    properties: {
        waitNode:cc.Node,
        waitCircle:cc.Node,
        loginGroup:cc.Node,
        // menuGroup:cc.Node,

        startBtn:cc.Button,

        rankBtn:cc.Node,
        optBtn:cc.Node,
        version:cc.Label,
    },

    // LIFE-CYCLE CALLBACKS:

    onLoad () {
        if( cc.sys.browserType == cc.sys.BROWSER_TYPE_WECHAT_GAME){
            //屏幕适配参数
            var info = wx.getSystemInfoSync();
            console.log("----------- ");
            console.log(info);
            // {model: "iPhone X", pixelRatio: 3, windowWidth: 375, windowHeight: 812, 
            userData.instance.mobileMode   = info.model;
            userData.instance.windowHeight = info.windowHeight;
            userData.instance.windowWidth  = info.windowWidth;
            if(info.windowHeight / info.windowWidth >= 2.1){ //iphoneX 等窄屏幕 全面屏幕
                userData.instance.isQMScreen   = true;
            }
            this.offset = 0;
            var percent = userData.instance.windowHeight/userData.instance.windowWidth;
            var percent_dev = 1280/720;
            var offset = (percent - percent_dev)/2 * 1280/2;
            userData.instance.offset = offset;
        }
        this.bannerAd = null;
    },
    
    start () {
        var obj = cc.find("Canvas");
        this.uiMgr = obj.getComponent("UIManager");
        this.uiMgr.addUI(this.node);

        // this.version.string = ""+userData.instance.version;

        this.waitNode.active = true;
        //设置右上角的分享按钮
        wxInterface.instance.showShareMenu();
        //微信游戏登录
        this.wxLogin();
        //检测更新
        wxInterface.instance.checkUpdate();
        //
        this.loginOriginPos = cc.v2(0,0);
        this.menuOriginPos  = cc.v2(0,0);
        this.rankOriginPos  = cc.v2(-138,-174);
        this.optOriginPos   = cc.v2(138, -174);
        // this.loginGroup.active = true;
        // this.menuGroup.active  = false;
        // this.loginMoveIn();
    },

    update (dt) {
        if(this.waitNode.active == true){
            this.waitCircle.rotation += 60*dt;
        }
    },
    //微信游戏登录
    wxLogin(){
        var _self = this;
        if( cc.sys.browserType == cc.sys.BROWSER_TYPE_WECHAT_GAME){
            if(userData.instance.isLogined == true && userData.instance.isGetedInfo == true){
                // _self.startBtn.node.active = true;
                // _self.loginGroup.active = true;
                // _self.menuGroup.active  = true;
                _self.waitNode.active  = false;
            }else {
                // _self.loginGroup.active = true;
                // _self.menuGroup.active  = false;
                // _self.startBtn.node.active = false;

                wxInterface.instance.wxLogin(function(){
                    userData.instance.isLogined = true;
                    _self.waitNode.active = false;
                    _self.node.active = false;
                    // //获取用户信息 或者 显示进入游戏按钮
                    // wxInterface.instance.getUserInfo(function(res){
                        
                    //     userData.instance.isGetedInfo = true;
                    //     userData.instance.nickName = res.userInfo.nickName;
                    //     userData.instance.gender   = res.userInfo.gender;
                    //     userData.instance.avatarUrl= res.userInfo.avatarUrl;
                    //     userData.instance.saveData();
                    //     this.clickPlayBtn();//点击开始按钮
                    // }.bind(this));
                }.bind(this), function(){

                }.bind(this));                
            }
            //检测更新
            wxInterface.instance.checkUpdate();
           
        }else {
            this.isLogined = true;
            this.waitNode.active   = false;
            this.node.active = false;
                
            //获取用户信息 或者 显示进入游戏按钮
            wxInterface.instance.getUserInfo(function(){
                
            }.bind(this));
        }
    },
    loginMoveIn(cb){
        var pos  = cc.v2(this.loginOriginPos.x, this.loginOriginPos.y - 200);
        var pos1 = cc.v2(this.rankBtn.x, this.rankBtn.y - 200);
        var pos2 = cc.v2(this.optBtn.x,  this.rankBtn.y - 200);
        this.loginGroup.setPosition(pos);
        this.rankBtn.setPosition(pos1);
        this.optBtn.setPosition(pos2);
        var act = cc.moveTo(0.5, this.loginOriginPos);
        var seq = cc.sequence(act, cc.callFunc(function(){
            if(cb){
                cb();
            }
        }.bind(this)));
        this.loginGroup.runAction(seq);
        var act1 = cc.moveTo(0.6, this.rankOriginPos);
        var act2 = cc.moveTo(0.6, this.optOriginPos);
        this.rankBtn.runAction(act1);
        this.optBtn.runAction(act2);
    },
    loginMoveOut(cb){
        var pos = cc.v2(-800, this.loginGroup.y);
        var act = cc.moveTo(0.5, pos);
        act.easing(cc.easeBackIn())
        var seq = cc.sequence(act, cc.callFunc(function(){
            if(cb){
                cb();
            }
        }.bind(this)));
        this.loginGroup.runAction(seq);
    },
    menuMoveIn(cb){
        // var pos = cc.v2(800, this.menuOriginPos.y);
        // this.menuGroup.setPosition(pos);
        // this.menuGroup.active = true;

        // var act = cc.moveTo(0.5, this.menuOriginPos);
        // act.easing(cc.easeBackOut())
        // var seq = cc.sequence(act, cc.callFunc(function(){
        //     if(cb){
        //         cb();
        //     }
        // }.bind(this)));
        // this.menuGroup.runAction(seq);
    },
    menuMoveOut(cb){
        // var pos = cc.v2(-800, this.menuOriginPos.y);
        
        // var act = cc.moveTo(0.5, pos);
        // act.easing(cc.easeBackIn())
        // var seq = cc.sequence(act, cc.callFunc(function(){
        //     if(cb){
        //         cb();
        //     }
        // }.bind(this)));
        // this.menuGroup.runAction(seq);

        // var pos1 = cc.v2(this.optBtn.x - 800,  this.optBtn.y);
        // var pos2 = cc.v2(this.rankBtn.x - 800, this.rankBtn.y);
        // var act1 = cc.moveTo(0.6, pos1);
        // var act2 = cc.moveTo(0.4, pos2);
        // act1.easing(cc.easeBackIn())
        // act2.easing(cc.easeBackIn())
        // this.rankBtn.runAction(act1);
        // this.optBtn.runAction(act2);
    },
    //点击开始按钮
    clickPlayBtn(){
        this.node.active = false;
        // this.loginMoveOut(function(){
        //     userData.instance.gameTimes++;
        //     this.uiMgr.showUI("WaitingUI");
        //     cc.director.loadScene("game")

        //     // this.menuMoveIn(function(){

        //     // }.bind(this));
        //     //护盾数量
        //     if(userData.instance.shieldNum >= 3)
        //         userData.instance.curTakeShieldNum = 3;
        //     else 
        //         userData.instance.curTakeShieldNum = userData.instance.shieldNum;
            
        // }.bind(this));
    },
    //无尽模式    玩法1 
    clickMenuBtn_1(){
        this.menuMoveOut(function(){
            userData.instance.gameTimes++;
            this.uiMgr.showUI("WaitingUI");
            cc.director.loadScene("game")
        }.bind(this));
    },
    //一个都不能死 玩法1 
    clickMenuBtn_2(){
        this.menuMoveOut(function(){
            userData.instance.gameTimes++;
            this.uiMgr.showUI("WaitingUI");
            cc.director.loadScene("game_1")
        }.bind(this));
    },
    
    //选项
    clickOptBtn(){
        this.uiMgr.showUI("OptionUI");
        this.hideBanner();
        if(wxInterface.instance.button)
            wxInterface.instance.button.hide();
    },
    //排行
    clickRankBtn(){ 
        var obj = cc.find("Canvas/RankUI");
        if(!obj){
            obj = this.uiMgr.showUI("RankUI");
        }
        if(obj.active == false){
            obj.active = true;
        }
        this.hideBanner();
        if(wxInterface.instance.button)
            wxInterface.instance.button.hide();
        
    },
    clickShareBtn(){
        wxInterface.instance.sendShareMsg("看谁跑得快！我是飞毛腿！", 'share_1.jpg');
    },
    onEnable(){
        console.log("全面屏：" + userData.instance.isQMScreen);
        // if(this.bannerAd){
        //     this.bannerAd.show();
        // }else {
        //     if(userData.instance.isQMScreen == true){
        //         this.bannerAd = wxInterface.instance.createBannerAD("adunit-0254438be741a414", 20, 120, 300);
        //     }else {
        //         this.bannerAd = wxInterface.instance.createBannerAD("adunit-0254438be741a414", 20, 100, 300);
        //     }
        // }
    },
    onDisable(){
        this.hideBanner();
    },
    showBanner(){
        // if(this.bannerAd){
        //     this.bannerAd.show();
        // }else {
        //     if(userData.instance.isQMScreen == true){
        //         this.bannerAd = wxInterface.instance.createBannerAD("adunit-0254438be741a414", 20, 120, 300);
        //     }else {
        //         this.bannerAd = wxInterface.instance.createBannerAD("adunit-0254438be741a414", 20, 100, 300);
        //     }
        // }
    },
    hideBanner(){
        // if(this.bannerAd){
        //     this.bannerAd.hide();
        //     this.bannerAd.destroy();
        //     this.bannerAd = null;
        //     // this.bannerAd = wxInterface.instance.createBannerAD("adunit-0f99c42f772841a0", 20, 100, 300);
        //     if(userData.instance.isQMScreen == true){
        //         this.bannerAd = wxInterface.instance.createBannerAD("adunit-0254438be741a414", 20, 120, 300);
        //     }else {
        //         this.bannerAd = wxInterface.instance.createBannerAD("adunit-0254438be741a414", 20, 100, 300);
        //     }
        //     this.bannerAd.hide();
        // }
    },

});
